package la.iok.aone.monitor.youtube.listener;

import la.iok.aone.monitor.youtube.event.ExcelResourceChangeEvent;
import la.iok.aone.monitor.youtube.service.YoutubeLinkService;
import la.iok.aone.monitor.youtube.task.ImportYoutubeExcel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 导入处理代理, 启用异步执行。
 * 具体导入任务的执行，由配置的导入客户端来实现。
 * @author bunco
 */
@Component
public class ImportHandler implements ApplicationListener<ExcelResourceChangeEvent> {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Value("${config.import.ignore:false}")
	private boolean ignoreImport;
	@Autowired
	private YoutubeLinkService youtubeLinkService;

	/**
	 * 异步执行，导入操作
	 * 具体导入操作，由配置文件配置导入客户端来执行，方便以后扩展。
	 */
	@Async
	public void onApplicationEvent(ExcelResourceChangeEvent event) {
		logger.info("Import ExcelResourceChangeEvent: {}", event.getSourceFile().getPath());
		if (event.getSourceFile().isDirectory() || ignoreImport) {
			logger.info("Import ExcelResourceChangeEvent: {} file is directory or ignore import: {}",
					event.getSourceFile().getPath(), ignoreImport);
			return;
		}

        ImportYoutubeExcel importYoutubeExcel = new ImportYoutubeExcel(this.youtubeLinkService, event.getSourceFile());
		importYoutubeExcel.run();;
	}
}
